package org.edu.model;


import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

/**<h2>科室树</h2>
 * <p>该表详细配置了科室与科室树one-to-one的关系</P>
 * 
 * @author 谢圣强
 * @version 1.1
 * @see Dept
 */
@Entity
@Table(name = "T_TreeNode")
public class TreeNode {
	private int id; // 编号
	private int pid; // 父节点
	private String name; //内容
	// private Children children;
	private List<TreeNode> children = new ArrayList<TreeNode>();

	public TreeNode() {
		super();
	}

	public TreeNode(int id, int pid) {
		super();
		this.id = id;
		this.pid = pid;
	}
/*
	// 先序遍历，拼接JSON字符串
	public String toString() {
		String result = "{id : '" + this.id + "'" + ", name : '"
				+ this.name + "'";

		// 拼接子节点
		// out.println("[{name: \'111\', children: [{id:'1',name: \'221\'},{name: \'332\'}]}]");
		if (children != null && children.size() != 0) {
			result += ", children : [";
			for (int i = 0; i < children.size() - 1; i++) {
				result += children.get(i).toString() + ',';
			}
			result += children.get(children.size() - 1).toString() + ']';

		}
		return result + '}';
	}
*/
	/*
	 * // 兄弟节点横向排序 public void sortChildren() { if (children != null &&
	 * children.getSize() != 0) { children.sortChildren(); } }
	 * 
	 * 
	 */
	//添加孩子节点 
	public void addChild(TreeNode node) {
		this.children.add(node); 
	}
	
	
	@Override
	public String toString() {
		return "{\'id\':" + id + ",\'pId\':" + pid + ",\'name\':\'" + name + "\'}";
	}

	@Id
	@GenericGenerator(name = "idGenerator", strategy = "assigned")
	@GeneratedValue(generator = "idGenerator")
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "tb_pid", nullable = true)
	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	
	@Column(name = "tb_name", nullable = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Transient
	public List<TreeNode> getChildren() {
		return children;
	}

	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}

}
